define([ 'jquery' ], function($) {

	var Util = function() {
	};
	
	Util.prototype = {
		copyBean : function(obj, newPropertys) {
			var newObj = {};
			for(var key in obj){
				newObj[key] = obj[key];
			}
			if(newPropertys) {
				for(var key in newPropertys){
					newObj[key] = newPropertys[key];
				}				
			}
			return newObj;
		},
		clearInput : function(obj) {
			if(typeof obj.data == "string") {
				obj.data = JSON.parse(obj.data);
			}
			if(!obj.root) {
				obj['root'] = $(document.body); 
			} else {
				obj['root'] = (typeof obj.root == "object" || obj.root.indexOf("#") != -1 || obj.root.indexOf(".") != -1) ? $(obj.root) :($("#"+obj.root) || $("."+obj.root) || $(obj.root));
			}
			obj.root.find("select, textarea, input[type='text'], input[type='hidden']").each(function() {
				$(this).val('');
			});
			obj.root.find("input[type='radio'], input[type='checkbox']").each(function() {
				$(this).attr("checked","");
			});
		},
		setHtml : function(root, data) {
			if(typeof data == "string") {
				data = JSON.parse(data);
			}
			root.find("div, span, label, h1, h2, h3, h4, h5, h6, li").each(function() {
				$(this).html(data[this.name]);
			});
		},
		setInput : function(obj) {
			if(typeof obj.data == "string") {
				obj.data = JSON.parse(obj.data);
			}
			if(!obj.root) {
				obj['root'] = $(document.body); 
			} else {
				obj['root'] = (typeof obj.root == "object" || obj.root.indexOf("#") != -1 || obj.root.indexOf(".") != -1) ? $(obj.root) :($("#"+obj.root) || $("."+obj.root) || $(obj.root));
			}
			obj.root.find("select, textarea, input[type='text'], input[type='hidden'], input[type='date'], input[type='tel']").each(function() {
				$(this).val(obj.data[this.name]);
			});
			obj.root.find("input[type='radio'], input[type='checkbox']").each(function() {
				if(obj.data[this.name] == $(this).val()) {
					$(this).attr("checked","checked");
				}
			});
			obj.root.find("img").each(function(){
				$(this).attr("src", ((obj.baseUrl || "") + obj.data[this.name]));
			});
		},
		getInput : function(root) {
			var dto = {};
			if(!root) {
				root = $(document.body); 
			} else {
				root = (typeof root == "object" || root.indexOf("#") != -1 || root.indexOf(".") != -1) ? $(root) :($("#"+root) || $("."+root) || $(root));
			}
			root.find("select, textarea, input[type='text'], input[type='hidden'], input[type='date'], input[type='tel']").each(function() {
				if(this.name && this.value) {
					dto[this.name] = this.value;
				}
			});
			root.find("input[type='radio'], input[type='checkbox']").each(function() {
				if($(this).attr("checked")) {
					dto[this.name] = this.value;
				}
			});
			return dto;
		},
		initTableCheckbox : function(id, name) {
			var checkbox = (typeof id == "string") ?  $("#" + id) : id;
			checkbox.change(function() {
				if(name) {
					$("td input[name='"+name+"']").prop("checked", checkbox.prop("checked"));
				} else {
					$("td input[type='checkbox']").prop("checked", checkbox.prop("checked"));
				}
			});
		},
		getNumberDate : function() {
			var d = new Date();
			var month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
			var date = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
			return d.getFullYear() + "" + month + "" + date;
		},
		stringToDate : function(str){/*'yyyy-MM-dd HH:mm:ss'格式的字符串转日期*/
		    var tempStrs = str.split(" ");
		    var dateStrs = tempStrs[0].split("-");
		    var year = parseInt(dateStrs[0], 10);
		    var month = parseInt(dateStrs[1], 10) - 1;
		    var day = parseInt(dateStrs[2], 10);
		    var timeStrs = tempStrs[1].split(":");
		    var hour = parseInt(timeStrs [0], 10);
		    var minute = parseInt(timeStrs[1], 10);
		    var second = parseInt(timeStrs[2], 10);
		    var date = new Date(year, month, day, hour, minute, second);
		    return date;
		},
		bindDompareData : function(startId, endId) {
			var $this = this;
			$('#'+startId+', #'+endId+'').bind("input propertychange change", function() {
		    	var startTime = $("#" + startId).val();
		    	var endTime = $("#" + endId).val();
		    	var currentTime = new Date().getTime();
		    	if(startTime && startTime.length > 0) {
		    		startTime = $this.stringToDate(startTime).getTime();
		    	}
		    	if(endTime && endTime.length > 0) {
		    		endTime = $this.stringToDate(endTime).getTime();
		    	}
		    	if(startTime && startTime > 0 && endTime && endTime > 0) {
		    		if(startTime >= endTime) {
		    			$.alert("开始时间不能大于等于结束时间");
		    			$(this).val("");
		    			return false;
		    		}
		    	} 
		    	if(startTime && startTime > 0) {
		    		if(startTime > currentTime) {
		    			$.alert("开始时间不能大于当前时间");
		    			$(this).val("");
		    			return false;
		    		}
		    	} 
		    	if(endTime && endTime > 0) {
		    		if(endTime > currentTime) {
		    			$.alert("选择的结束时间不能大于当前时间");
		    			$(this).val("");
		    			return false;
		    		}
		    	} 

		    });		
		},
		parseLongDate : function(longDate) {
			if(!longDate) return "";
			var d = new Date(longDate);
			var month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
			var date = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
			return d.getFullYear() + "-" + month + "-" + date;
		},
		parseLongDateTime : function(longDate) {
			if(!longDate) return "";
			var d = new Date(longDate);
			var hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
			var minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
			var seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
			return this.parseLongDate(longDate) + " " + hours + ":" + minutes + ":" + seconds;
		},
		selectPadding : function(obj) {
			var data = obj.data;
			var options = '';
			if(obj.firstValue != null && obj.firstName != null) {
				options += '<option value="'+obj.firstValue+'">'+obj.firstName+'</option>';
			}
			for(var i in data) {
				options += '<option value="'+i+'">'+data[i]+'</option>';
			}
			if(typeof obj.id == "string") {
				obj.id = $("#" + obj.id);
			}
			$(obj.id).html(options);
			if(obj.selectValue != null) {
				$(obj.id).val(obj.selectValue);
			}
		},
		stringify_aoData : function(aoData) {
			var o = {};
			var modifiers = [ 'mDataProp_', 'sSearch_', 'iSortCol_','bSortable_', 'bRegex_', 'bSearchable_','sSortDir_' ];
			for(var i in aoData) {
				// TODO
			}
			$.each(aoData, function(idx, obj) {
				if (obj.name) {
					for (var i = 0; i < modifiers.length; i++) {
						if (obj.name.substring(0, modifiers[i].length) == modifiers[i]) {
							var index = parseInt(obj.name.substring(modifiers[i].length));
							var key = 'a' + modifiers[i].substring(0,modifiers[i].length - 1);
							if (!o[key]) {
								o[key] = [];
							}
							// console.log('index=' + index);
							o[key][index] = obj.value;
							// console.log(key +".push(" + obj.value +")");
							return;
						}
					}
					// console.log(obj.name+"=" +
					// obj.value);
					o[obj.name] = obj.value;
				} else {
					o[idx] = obj;
				}
			});
			return JSON.stringify(o);
		},

		set_menu : function(item) {
			$('.navbar-fixed-top .navbar-nav li').removeClass('active');
			$('.navbar-fixed-top .navbar-nav li').each(function(i) {
				var menuItem = $(this).attr('data-menu-item');
				if (menuItem == item) {
					$(this).addClass('active');
				}
			});
		},

		ajax_submit : function(form) {
			var o = {};
			$(form).find('input,textarea,select').each(function() {
				var $this = $(this);
				var key = $this.attr('name');
				var val = $this.val();
				if (key) {
					if(this.type == "checkbox"){
						if(this.checked){
							if(o[key]){
								if($.isArray(o[key])){
									o[key].push(val);
								}else{
									o[key]=[o[key],val];
								}
							}else{
								o[key] = val;
							}
						}
					}else{
						o[key] = val;
					}
				}
			});
			return $.ajax({
				url : $(form).attr('action'),
				type : $(form).attr('method'),
				dataType : 'json',
				headers : {
					'x-form-id' : $(form).attr('id')
				},
				contentType : 'application/json; charset=UTF-8',
				data : JSON.stringify(o),
				beforeSend:function(){
					$("#mask").css("height",$(document).height());     
			        $("#mask").css("width",$(document).width());     
			        $("#mask").show();
				},
				error:function(){
					 $("#mask").hide();   
				},
				complete:function(){
					 $("#mask").hide();   
				}
			});
		},

		redirect : function(url) {
			// Similar behavior as an HTTP redirect
			window.location.replace(url);
		},

		getSearchData : function(containerId) {
			var result = [];
			$(containerId).find('input,textarea,select').each(function() {
				var o = {};
				var key;
				if ($(this).attr("data-ignore") == "true") {
					return true;
				}
				if ($(this).hasClass("select2-focusser select2-offscreen") || $(this).hasClass("select2-input")) {
					return true;
				}
				key = $(this).attr('name');
				if (key) {
					if ($(this).attr("Type") == "checkbox") {
						o["name"] = key;
						if ($(this).val() == "true") {
							o["value"] = true;
						} else if ($(this).val() == "false"){
							o["value"] = false;
						} else {
							o["value"] = $(this).val();
						}
					} else if ($(this).attr("Type") == "radio") {
						if ($(this).is(":checked")) {
							o["name"] = key;
							o["value"] = $(this).val();
						} else {
							return;
						}
					} else {
						o["name"] = key;
						o["value"] = $(this).val();
					}
					result.push(o);
				}
			});
			return result;
		}
	};
	return new Util();
});